home *** CD-ROM | disk | FTP | other *** search
/ OpenGL Superbible (2nd Edition) / OpenGL SuperBible e2.iso / tools / GLUT-3.7 / PROGS / DEMOS / GEOFACE / DISPLAY.C next >
Encoding:
C/C++ Source or Header  |  1998-08-12  |  8.5 KB  |  314 lines

  1. /* ==========================================================================
  2.                             DISPLAY_C
  3. =============================================================================
  4.  
  5.     FUNCTION NAMES
  6.  
  7.     void paint_muscles             -- displays the muscles.
  8.     void paint_polyline         -- paint the polyline.
  9.     paint_polygons            -- display the polygons.
  10.     calculate_polygon_vertex_normal     -- calculate the vertex norms.
  11.     calc_normal             -- calculate a normal.
  12.  
  13.     C SPECIFICATIONS
  14.  
  15.     void paint_muscles             ( HEAD *face )
  16.     void paint_polyline         ( HEAD *face )
  17.     paint_polygons              ( HEAD *face, int type, int normals )
  18.     calculate_polygon_vertex_normal     ( HEAD *face ) 
  19.     calc_normal             ( float *p1, float *p2, float *p3, 
  20.                                           float *norm )
  21.  
  22.     DESCRIPTION
  23.     
  24.     This module is responsible for displaying the face geometry.
  25.     This module comes as is with no warranties.  
  26.  
  27.     HISTORY
  28.     16-Dec-94  Keith Waters (waters) at DEC's Cambridge Research Lab
  29.     Created.
  30.  
  31. ============================================================================ */
  32.  
  33. #include <math.h>           /* C header for any math functions               */
  34. #include <stdio.h>          /* C header for standard I/O                     */
  35. #include <string.h>         /* For String compare                            */
  36. #include <stdlib.h>
  37. #ifndef _WIN32
  38. #include <sys/types.h>
  39. #include <sys/file.h>
  40. #endif
  41. #include <GL/glut.h>        /* OpenGl headers                     */
  42.  
  43. #include "head.h"       /* local header for the face                     */
  44.  
  45. void calc_normal ( float *p1, float *p2, float *p3, float *norm );
  46.  
  47. /* ========================================================================= */  
  48. /* paint_muscles                                                             */
  49. /* ========================================================================= */  
  50. /*
  51. ** Displays the face muscles.
  52. **
  53. */
  54.  
  55. #define PAINT_MUSCLES_DEBUG 0
  56. void paint_muscles ( HEAD *face )
  57. {
  58.   int i,j;
  59.   float v1[3], v2[3] ;
  60.  
  61.   glLineWidth   ( 3.0 ) ;
  62.   glColor3f     ( 100.0, 200.0, 200.0 ) ;
  63.  
  64.     for ( i=0; i<face->nmuscles; i++ ) {
  65.     
  66.       for (j=0; j<3; j++) {
  67.       v1[j] = face->muscle[i]->head[j] ;
  68.       v2[j] = face->muscle[i]->tail[j] ;
  69.     }
  70.       
  71. #if PAINT_MUSCLES_DEBUG
  72.     fprintf (stderr, "head x: %f y: %f z: %f\n",   v1[0], v1[1], v1[2] ) ;
  73.     fprintf (stderr, "tail x: %f y: %f z: %f\n\n", v2[0], v2[1], v2[2] ) ;
  74. #endif
  75.  
  76.       glBegin ( GL_LINE_STRIP ) ;
  77.       glVertex3f ( v1[0], v1[1], v1[2] ) ;
  78.       glVertex3f ( v2[0], v2[1], v2[2] ) ;
  79.       glEnd ( ) ;
  80.   }
  81.   glLineWidth   ( 1.0 ) ;
  82. }
  83.  
  84. /* ========================================================================= */  
  85. /* paint_polyline                                                            */
  86. /* ========================================================================= */  
  87. /*
  88. ** Displays the polyline.
  89. **
  90. */
  91.  
  92. void paint_polyline ( HEAD *face )
  93. {
  94.   int i,j,cnt ;
  95.   float v1[3] ;
  96.   static float r ;
  97.  
  98.   glClear        ( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ) ;
  99.   glLineWidth   ( 1.0 ) ;
  100.   glColor3f     ( 100.0, 100.0, 0.0 ) ;
  101.   
  102.   glPushMatrix  ( ) ;
  103.   glRotatef    ( r, 1.0, 1.0, 1.0 ) ;
  104.  
  105.   glBegin ( GL_LINE_STRIP ) ;
  106.     for (cnt=0, i=0; i<face->npolylinenodes; i++ ) {
  107.     
  108.     for (j=0; j<3; j++, cnt++) 
  109.       v1[j] = face->polyline[cnt] ;
  110.  
  111. #if PAINT_POLYLINE_DEBUG
  112.     printf ("x: %f y: %f z: %f\n", v1[0], v1[1], v1[2] ) ;
  113. #endif
  114.  
  115.       glVertex3f ( v1[0], v1[1], v1[2] ) ;
  116.   }
  117.   
  118.   glEnd ( ) ;
  119.  
  120.   glPopMatrix    ( ) ;
  121.   glFlush    ( ) ;
  122.   
  123. }
  124.  
  125.  
  126. /* ========================================================================= */  
  127. /* paint_polygons                                                            */
  128. /* ========================================================================= */  
  129. /*
  130. ** Paints the polygons of the face. 
  131. ** Type indicates if they are to be 
  132. ** drawn         (type=0), 
  133. ** flat shaded   (type=1),
  134. ** smooth shaded (type=2).
  135. */
  136.  
  137. void paint_polygons ( HEAD *face, int type, int normals )
  138. {
  139.   int    i, j ;
  140.   float  v1[3], v2[3], v3[3] ;
  141.   float  norm1[3], norm2[3], norm3[3] ;
  142.   float  vn1[3], vn2[3], vn3[3] ;
  143.  
  144.   glLineWidth   ( 2.0 ) ;  
  145.  
  146.   for (i=0; i<face->npolygons; i++ )
  147.     {
  148.       for (j=0; j<3; j++) {
  149.     v1[j] = face->polygon[i]->vertex[0]->xyz[j] ;
  150.     v2[j] = face->polygon[i]->vertex[1]->xyz[j] ;
  151.     v3[j] = face->polygon[i]->vertex[2]->xyz[j] ;
  152.       }
  153.  
  154.       if ( type == 0 ) {
  155.     
  156.     for (j=0; j<3; j++) {
  157.       norm1[j] = face->polygon[i]->vertex[0]->norm[j] ;
  158.       norm2[j] = face->polygon[i]->vertex[1]->norm[j] ;
  159.       norm3[j] = face->polygon[i]->vertex[2]->norm[j] ;
  160.     }
  161.     glBegin ( GL_LINE_LOOP ) ; {
  162.       glNormal3f ( norm1[0], norm1[1], norm1[2] ) ;
  163.       glVertex3f ( v1[0],    v1[1],    v1[2]    ) ;
  164.       glNormal3f ( norm2[0], norm2[1], norm2[2] ) ;
  165.       glVertex3f ( v2[0],    v2[1],    v2[2]    ) ;
  166.       glNormal3f ( norm3[0], norm3[1], norm3[2] ) ;
  167.       glVertex3f ( v3[0],    v3[1],    v3[2]    ) ;
  168.     }  glEnd ( ) ;
  169.  
  170.       } /* end if drawn */
  171.  
  172.       if ( type == 1 ) {
  173.     
  174.     for (j=0; j<3; j++) {
  175.       norm1[j] = face->polygon[i]->vertex[0]->norm[j] ;
  176.       norm2[j] = face->polygon[i]->vertex[1]->norm[j] ;
  177.       norm3[j] = face->polygon[i]->vertex[2]->norm[j] ;
  178.     }
  179.     glBegin ( GL_TRIANGLES ) ; {
  180.       glNormal3f ( norm1[0], norm1[1], norm1[2] ) ;
  181.       glVertex3f ( v1[0],    v1[1],    v1[2]    ) ;
  182.       glNormal3f ( norm2[0], norm2[1], norm2[2] ) ;
  183.       glVertex3f ( v2[0],    v2[1],    v2[2]    ) ;
  184.       glNormal3f ( norm3[0], norm3[1], norm3[2] ) ;
  185.       glVertex3f ( v3[0],    v3[1],    v3[2]    ) ;
  186.     }  glEnd ( ) ;
  187.  
  188.       } /* end if drawn */
  189.  
  190.  
  191.       else if ( type == 1) {
  192.     for (j=0; j<3; j++) {
  193.       norm1[j] = face->polygon[i]->vertex[0]->norm[j] ;
  194.       norm2[j] = face->polygon[i]->vertex[1]->norm[j] ;
  195.       norm3[j] = face->polygon[i]->vertex[2]->norm[j] ;
  196.     }
  197.       } /* end if flat */
  198.  
  199.       else if ( type == 2 ) {
  200.  
  201.     averaged_vertex_normals ( face, i, norm1, norm2, norm3 ) ;
  202.  
  203.       } /* end if smoothed */
  204.  
  205.       if ( type ) {
  206.  
  207.     glBegin ( GL_TRIANGLES ) ; {
  208.       glNormal3f ( norm1[0], norm1[1], norm1[2] ) ;
  209.       glVertex3f ( v1[0],    v1[1],    v1[2]    ) ;
  210.       glNormal3f ( norm2[0], norm2[1], norm2[2] ) ;
  211.       glVertex3f ( v2[0],    v2[1],    v2[2]    ) ;
  212.       glNormal3f ( norm3[0], norm3[1], norm3[2] ) ;
  213.       glVertex3f ( v3[0],    v3[1],    v3[2]    ) ;
  214.     }  glEnd ( ) ;
  215.       } /* endif painted */
  216.  
  217.       if ( normals ) {
  218.     for (j=0; j<3; j++) {
  219.       vn1[j] = face->polygon[i]->vertex[0]->xyz[j] + norm1[j] ;
  220.       vn2[j] = face->polygon[i]->vertex[1]->xyz[j] + norm2[j] ;
  221.       vn3[j] = face->polygon[i]->vertex[2]->xyz[j] + norm3[j] ;
  222.     }
  223.  
  224.     glBegin ( GL_LINE_STRIP ) ; {
  225.       glVertex3f ( v1[0],    v1[1],    v1[2]     ) ;
  226.       glVertex3f ( vn1[0],  vn1[1],    vn1[2]    ) ;
  227.     }  glEnd ( ) ;
  228.  
  229.  
  230.     glBegin ( GL_LINES ) ; {
  231.       glVertex3f ( v2[0],    v2[1],    v2[2]     ) ;
  232.       glVertex3f ( vn2[0],  vn2[1],    vn2[2]    ) ;
  233.     }  glEnd ( ) ;
  234.  
  235.  
  236.     glBegin ( GL_LINES ) ; {
  237.       glVertex3f ( v3[0],    v3[1],    v3[2]     ) ;
  238.       glVertex3f ( vn3[0],  vn3[1],    vn3[2]    ) ;
  239.     }  glEnd ( ) ;
  240.  
  241.       }
  242.     }
  243.   glLineWidth   ( 1.0 ) ;  
  244.  
  245. /* ========================================================================= */  
  246. /* calculate_polygon_vertex_normal.                         */
  247. /* ========================================================================= */
  248. /*
  249. ** As it says.
  250. */
  251.  
  252. void
  253. calculate_polygon_vertex_normal ( HEAD *face ) 
  254. {
  255.   int i,j,k ;
  256.   float p1[3], p2[3], p3[3] ;
  257.   float norm[3] ;
  258.   for (i=0; i<face->npolygons; i++ )
  259.     {
  260.       for (j=0; j<3; j++) 
  261.     p1[j] = face->polygon[i]->vertex[0]->xyz[j] ;
  262.       for (j=0; j<3; j++) 
  263.     p2[j] = face->polygon[i]->vertex[1]->xyz[j] ;
  264.       for (j=0; j<3; j++) 
  265.     p3[j] = face->polygon[i]->vertex[2]->xyz[j] ;
  266.  
  267.       calc_normal ( p1, p2, p3, norm ) ;
  268.  
  269.       for (j=0; j<3; j++) 
  270.     for (k=0; k<3; k++)
  271.       face->polygon[i]->vertex[j]->norm[k] = norm[k] ;
  272.     }
  273. }
  274.  
  275. /* ========================================================================= */  
  276. /* calc_normal.                                          */
  277. /* ========================================================================= */
  278. /*
  279. ** Calculates the normal vector from three vertices.
  280. */
  281. void
  282. calc_normal ( float *p1, float *p2, float *p3, float *norm )
  283. {
  284.   float coa, cob, coc ;
  285.   float px1, py1, pz1 ;
  286.   float px2, py2, pz2 ;
  287.   float px3, py3, pz3 ;
  288.   
  289.   float absvec ;
  290.   
  291.   px1 = p1[0] ;
  292.   py1 = p1[1] ;
  293.   pz1 = p1[2] ;
  294.   
  295.   px2 = p2[0] ;
  296.   py2 = p2[1] ;
  297.   pz2 = p2[2] ;
  298.   
  299.   px3 = p3[0] ;
  300.   py3 = p3[1] ;
  301.   pz3 = p3[2] ;
  302.   
  303.   coa = -(py1 * (pz2-pz3) + py2*(pz3-pz1) + py3*(pz1-pz2)) ;
  304.   cob = -(pz1 * (px2-px3) + pz2*(px3-px1) + pz3*(px1-px2)) ;
  305.   coc = -(px1 * (py2-py3) + px2*(py3-py1) + px3*(py1-py2)) ;
  306.   
  307.   absvec = sqrt ((double) ((coa*coa) + (cob*cob) + (coc*coc))) ;
  308.   
  309.   norm[0] = coa/absvec ;
  310.   norm[1] = cob/absvec ;
  311.   norm[2] = coc/absvec ;
  312. }
  313.